package com.niodata.dt.fs;

import com.niodata.dt.fs.us3.ConfigKeys;
import com.niodata.dt.fs.us3.Us3FileSystem;
import java.io.IOException;
import java.net.URI;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;


public class Us3FileSystemBuilder implements FileSystemBuilder {

  @Override
  public FileSystem createFileSystem(String user, Map<String, String> conf) throws IOException {
    Configuration config = new Configuration();
    config.set(ConfigKeys.ACCESS_KEY, conf.getOrDefault(ConfigKeys.ACCESS_KEY, ""));
    config.set(ConfigKeys.SECRET_KEY, conf.getOrDefault(ConfigKeys.SECRET_KEY, ""));
    config.set(ConfigKeys.REGION, conf.getOrDefault(ConfigKeys.REGION, ""));
    config.set(ConfigKeys.BUCKET, conf.getOrDefault(ConfigKeys.BUCKET, ""));
    config.set(ConfigKeys.ENDPOINT, conf.getOrDefault(ConfigKeys.ENDPOINT, ""));
    config.set(ConfigKeys.PROTOCOL, conf.getOrDefault(ConfigKeys.PROTOCOL, "https"));
    config.setClass("fs.us3.impl", Us3FileSystem.class, FileSystem.class);
    Us3FileSystem fileSystem = new Us3FileSystem();
    String bucket = conf.getOrDefault(ConfigKeys.BUCKET, "");
    String endPoint = conf.getOrDefault(ConfigKeys.ENDPOINT, "");
    String region = conf.getOrDefault(ConfigKeys.REGION, "");
    URI uri = URI.create("us3://" + bucket + "." + region + "." + endPoint);
    fileSystem.initialize(uri, config);
    return fileSystem;
  }

}
